<?php
session_start();
$typeScheme=$_POST['typeScheme'];
$frm=$_POST['frmId'];
include_once('connection/connection.php');
if(isset($_POST['oh_id']))
{
$check_trac=0;
if($_POST['status_site']=="NON TRACABLE")
{
$qr="update tbl_dtls_q1_oh set status_e_nt='$_POST[status_site]',status_covered='0',HP='$_POST[hp_num]',SP='$_POST[sp_num]' where id='$_POST[oh_id]'";
$check_trac=1;
}else{
$qr="update tbl_dtls_q1_oh set status_e_nt='$_POST[status_site]',status_covered='$_POST[status_cov]',HP='$_POST[hp_num]',SP='$_POST[sp_num]' where id='$_POST[oh_id]'";
}
$res=mysql_query($qr);

	if($_POST['status_site']!="NON TRACABLE" || ($_POST['status_site']=="NON TRACABLE" && $_POST['pre_exist_status']!="NON TRACABLE")){
	
		if($_POST['status_cov']==0 && $_POST['pre_status_cov']==1) {
		$data = mysql_fetch_assoc(mysql_query("SELECT district_id , total_population from tbl_lookup_location where habitation_code = (select habit_code from tbl_dtls_q1_oh where id= '$_POST[oh_id]') "));
		 mysql_query("UPDATE tbl_dtls_q1_main SET population_benefited = (population_benefited - $data[total_population]) , present_population = ( population_benefited * (select population_factor from tbl_lookup_pop_factor where district_id = '$data[district_id]'))   WHERE id = '$frm'");
		//echo $_SESSION['frm_unique_id'];
		}
		else if($_POST['status_cov']==1 && $_POST['pre_status_cov']==0) {
		$data = mysql_fetch_assoc(mysql_query("SELECT district_id , total_population from tbl_lookup_location where habitation_code = (select habit_code from tbl_dtls_q1_oh where id= '$_POST[oh_id]') "));
		mysql_query("UPDATE tbl_dtls_q1_main SET population_benefited = (population_benefited + $data[total_population]) , present_population = ( population_benefited * (select population_factor from tbl_lookup_pop_factor where district_id = '$data[district_id]'))    WHERE id = '$frm'");
		
		}
	}
	
	
	////////////////////////code for maintaining user history///////////////////
if($typeScheme=="P&T" || $typeScheme=="TSS(P&T)")
{
	$qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$frm','2','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
}
if($typeScheme=="Piped")
{
	$qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$frm','28','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
}
if($typeScheme=="Sanitary Diggs")
{
	echo $qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$frm','54','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
}


mysql_query($qryHistory);
///////////////////////////////
	
	if($res)
	{
	$qrhpsp_num="select sum(HP) as total_hp,sum(SP) as total_sp from tbl_dtls_q1_oh where loc_id ='$frm'";
		$reshpsp_num=mysql_query($qrhpsp_num);
		$rowhpsp=mysql_fetch_assoc($reshpsp_num);
		$hp_num=$rowhpsp['total_hp'];
		$sp_num=$rowhpsp['total_sp'];
		
	$qrhpsp_num="UPDATE tbl_dtls_q1_main SET hp_nos='$hp_num',sp_nos='$sp_num' where id = '$frm'";
	$reshpsp_num=mysql_query($qrhpsp_num);
	header("location:q1_rwss_edit.php");
	}
}
?>
